package com.itheima.udp2;

import javax.lang.model.element.NestingKind;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class FasongClient {

    public static void main(String[] args) throws IOException {
        //创建发送对象
        final DatagramSocket ds = new DatagramSocket(9999);
        Runnable runnable = new Runnable() {
            public void run() {
                try {
                    while (true) {
                        //创建数据对象
                        String s=new Scanner(System.in).nextLine();
                        byte[] bytes = s.getBytes();
                        InetAddress ip = InetAddress.getByName("zhaichaojie");
                        int port =8888;
                        DatagramPacket dp = new DatagramPacket(bytes,bytes.length,ip,port);
                        // 发送
                        ds.send(dp);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
new Thread(runnable).start();
        Runnable runnable1 = new Runnable() {
            public void run() {
                while (true) {
                    byte[] bytes1= new byte[1024*64];
                    DatagramPacket jieshoudp = new DatagramPacket(bytes1, bytes1.length);
                    try {
                        ds.receive(jieshoudp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String s1 = new String(bytes1,0, jieshoudp.getLength());
                    System.out.println(s1);
                }
            }
        };
new Thread(runnable1).start();
        //释放资源
      //ds.close();


    }
}
